//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;

namespace OpenLS.UI
{
    partial class PopupServiceResources
    {
        public PopupServiceResources()
        {
            InitializeComponent();
        }

        private static readonly PopupServiceResources instance = new PopupServiceResources();
        public static ControlTemplate PopupContentControl { get { return (ControlTemplate) instance["popupContentControl"]; } }



        public static Point GetOffset(DependencyObject obj)
        {
            return (Point)obj.GetValue(OffsetProperty);
        }

        public static void SetOffset(DependencyObject obj, Point value)
        {
            obj.SetValue(OffsetProperty, value);
        }

        // Using a DependencyProperty as the backing store for Offset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OffsetProperty =
            DependencyProperty.RegisterAttached("Offset", typeof(Point), typeof(PopupServiceResources), new UIPropertyMetadata(new Point()));


        private void Rectangle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = (FrameworkElement)sender;
            Mouse.Capture(element);
            SetOffset(element, e.GetPosition(element));
        }

        private void Rectangle_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Mouse.Capture(null);
            FrameworkElement element = (FrameworkElement)sender;
            element.ClearValue(OffsetProperty);
        }

        private void Rectangle_MouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.Captured != sender)
                return;
            FrameworkElement element = (FrameworkElement) sender;
            Popup popup = GetPopup(element);
            Point pt = e.GetPosition(popup.PlacementTarget);
            Point offset = GetOffset(element);
            pt.Offset(-offset.X, -offset.Y);
            popup.PlacementRectangle = new Rect(pt, new Size(popup.ActualWidth, popup.ActualHeight));
            popup.Placement = PlacementMode.RelativePoint;
        }

        static Popup GetPopup(object sender)
        {
            FrameworkElement b = (FrameworkElement) sender;
            ContentControl c = (ContentControl) b.TemplatedParent;
            return (Popup)c.Parent;
        }
    }
}
